package mylab.utils.rxtx;

import com.google.common.base.Strings;
import mylab.utils.common.OsUtil;
import mylab.utils.common.ShellUtil;

/**
 * File Name             :  RxtxUtils
 *
 * @author :  sylar
 * Create                :  2020/7/22
 */
public class RxtxUtils {

    /**
     * 系统授权
     *
     * @param rxtxPath
     */
    public static void preparePermisson(String rxtxPath) {

        System.setProperty("gnu.io.rxtx.SerialPorts", rxtxPath);
        String cmd;
        ShellUtil.CommandResult cmdRes;

        if (OsUtil.isLinux() || OsUtil.isMacOS() || OsUtil.isMacOSX()) {
            cmd = String.format("chmod 666 %s", rxtxPath);
            cmdRes = ShellUtil.execCommand(cmd, true, true);
            if (!Strings.isNullOrEmpty(cmdRes.errorMsg)) {
                throw new SecurityException("没有串口读写权限");
            }
        }

        if (OsUtil.isLinux()) {
            cmd = String.format("chmod 777 %s", "/data/local/tmp");
            cmdRes = ShellUtil.execCommand(cmd, true, true);
            if (!Strings.isNullOrEmpty(cmdRes.errorMsg)) {
                throw new SecurityException("没有android root权限");
            }
        }
    }
}
